package org.smith.docker.sample.service;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 堆内存溢出
 *
 * -Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError
 */
@Component
public class HeapOOMService {

    private AtomicInteger index = new AtomicInteger(0);

    private List<User> oomList = new ArrayList<>();

    public void heapOOM() {
        while (true) {
            oomList.add(new User(index.getAndAdd(1)));
        }
    }

    private static class User {
        private int id;

        public User(int id) {
            this.id = id;
        }
    }
}
